#include "palmus.h"
#include "ui_palmus.h"
#include <QDebug>
#include <list>
#include <windows.h>
#include <QtMath>
#include <QMovie>
#include <QTimer>
using namespace std;
typedef list<int> LISTINT;
static LISTINT listdate;
static LISTINT::iterator i,j;
static LISTINT ibidate;
static LISTINT::iterator k,l;
QMovie* movie;
static int bmp = 599;
Palmus::Palmus(QWidget *parent) :QWidget(parent),
    ui(new Ui::Palmus)
{
    ui->setupUi(this);
    this->setWindowTitle("Palmus");
    ui->label_palmus->setStyleSheet("background-color: rgb(0, 0, 0)");
    ui->label_palmus->installEventFilter(this);
    ui->label_ibi->installEventFilter(this);
    connect(&Demodate,SIGNAL(send_date(int,int,int,int,int)),this,SLOT(slot_get_date(int,int,int,int,int)));
    connect(this,SIGNAL(get_date_flag(int)),&Demodate,SLOT(change_get_flag(int)));
    movie = new QMovie("D:/WorkSpace/Palmus/source/heart_gif_2.gif");
    ui->label_heart->setMovie(movie);
    movie->start();
    Demodate.run();
    ui->label_blue->hide();
    ui->label_blue_date->hide();
    ui->label_internet->hide();
    ui->label_internet_date->hide();
    ui->label_serial->hide();
    ui->label_serial_date->hide();
}

Palmus::~Palmus()
{
    movie->stop();
    Demodate.exit(1);
    delete ui;
}
static bool serial_flag = true;
static bool interent_flag = true;
static bool blue_flag = true;
void Palmus::slot_get_date(int temp1, int temp2, int temp3, int temp4, int temp5)
{
    switch (temp1) {
    case 0:
    {
        ui->label_blue->hide();
        ui->label_blue_date->hide();
        ui->label_internet->hide();
        ui->label_internet_date->hide();
        ui->label_serial->show();
        if(serial_flag)
        {
            ui->label_serial_date->show();
            serial_flag = false;
        }
        else
        {
            ui->label_serial_date->hide();
            serial_flag = true;
        }
        listdate.push_front(0-(temp2-512));
        bmp = temp3;
        if(temp5 ==1)
        {
            ibidate.push_front(temp4);

        }
        update();
        if(listdate.size()>999)
        {
            listdate.pop_back();
        }
        if(ibidate.size()>999)
        {
            ibidate.pop_back();
        }
        break;
    }
    case 1:
    {
        ui->label_blue->hide();
        ui->label_blue_date->hide();
        ui->label_internet->show();
        ui->label_serial->hide();
        ui->label_serial_date->hide();
        if(interent_flag)
        {
            ui->label_internet_date->show();
            interent_flag = false;
        }
        else
        {
            ui->label_internet_date->hide();
            interent_flag = true;
        }
        listdate.push_front(temp2);
        update();
        if(listdate.size()>999)
        {
            listdate.pop_back();
        }
        break;
    }
    case 2:
    {
        ui->label_blue->show();
        ui->label_internet->hide();
        ui->label_internet_date->hide();
        ui->label_serial->hide();
        ui->label_serial_date->hide();
        if(blue_flag)
        {
            ui->label_blue_date->show();
            blue_flag = false;
        }
        else
        {
            ui->label_blue_date->hide();
            blue_flag = true;
        }
        listdate.push_front(temp2);
        update();
        if(listdate.size()>999)
        {
            listdate.pop_back();
        }
        break;
    }
    default:
        break;
    }
}

bool Palmus::eventFilter(QObject *object1, QEvent *event1)
{
    if(object1 == ui->label_palmus&&event1->type()==QEvent::Paint)
    {
        draw_palmus();
    }
    if(object1 == ui->label_ibi&&event1->type()==QEvent::Paint)
    {
        draw_ibi();
    }
    return true;
}

static int temp1;
static int temp2;
void Palmus::draw_palmus()
{
    //Draw scale
    QPainter painter_scale(ui->label_palmus);
    painter_scale.setBrush(Qt::black);
    painter_scale.drawRect(0,0,ui->label_palmus->width(),ui->label_palmus->height());
    painter_scale.setPen(QPen(Qt::green,0.5));
    painter_scale.setRenderHints(QPainter::Antialiasing,true);
    int scale_x = ui->label_palmus->width();
    int scale_y = ui->label_palmus->height();
    painter_scale.drawLine(0,0,0,scale_y);
    painter_scale.drawLine(0,scale_y,scale_x,scale_y);
    while (scale_y>0)
    {
        painter_scale.drawLine(0,scale_y,ui->label_palmus->width(),scale_y);
        scale_y = scale_y-20;
    }
    scale_x = 0;
    scale_y = ui->label_palmus->height();
    while (scale_x<(ui->label_palmus->width()))
    {
        painter_scale.drawLine(scale_x,scale_y,scale_x,0);
        scale_x= scale_x+20;
    }
    //Draw palmus
    QPainter painter(ui->label_palmus);
    painter.setRenderHints(QPainter::Antialiasing,true);
    painter.translate(0,(ui->label_palmus->height()/2));
    painter.save();
    painter.setPen(QPen(Qt::red,2));
    int x = ui->label_palmus->width();
    i = listdate.begin();
    temp1 = *i;
    for(i=listdate.begin();i!=listdate.end();i=i.operator ++(1))
    {
        j=i.operator ++(1);
        temp2 =  *j;
        painter.drawLine(x,(ui->Slider_scale->value()/100.0)*(temp1/1024.0*ui->label_palmus->height()),x-3,(ui->Slider_scale->value()/100.0)*(temp2/1024.0*ui->label_palmus->height()));
        temp1 = temp2;
        x=x-3;
    }
    bmp = min(bmp,200);
    ui->label_heart_rate->setText("BMP："+QString::number(bmp,10));
    painter.restore();

}

void Palmus::draw_ibi()
{
    QPainter painter(ui->label_ibi);
    painter.setPen(QPen(Qt::red,2));
    int x = ui->label_ibi->width();
    k = ibidate.begin();
    temp1 = *k;
    float temp3 =1024.0;
    for(k=ibidate.begin();k!=ibidate.end();k=k.operator ++(1))
    {
        l=k.operator ++(1);
        temp2 =  *l;
        if(temp1>temp3)
        {
            temp3 = temp1/1.0;
        }
        painter.drawLine(x,(temp1/temp3*ui->label_ibi->height()),x-1,(temp2/temp3*ui->label_ibi->height()));
        temp1 = temp2;
        x=x-1;
    }
    ui->label_ibi_date->setText("IBI："+QString::number(*ibidate.begin(),10));
}

//void Palmus::draw_heart()
//{



//    QPainter painter(ui->label_heart);
//    double x, y, r;
//    for (int i = 0; i <= 90; i++)
//    {
//        for (int j = 0; j <= 90; j++)
//        {
//            //转换为直角坐标系，设置偏移量，使图像居中
//            r = M_PI / 45 * i * (1 - qSin(M_PI / 45 * j)) * 19;
//            x = r * qCos(M_PI / 45 * j) * qSin(M_PI / 45 * i) + 100 / 2;
//            y = -r * qSin(M_PI / 45 * j) + 100 / 4;
//            painter.setBrush(Qt::red);
//            painter.setPen(Qt::red);
//            painter.drawPoint(x,y);
//        }
//    }
//}

void Palmus::on_pushButton_clicked()
{
    listdate.clear();
    update();
    emit get_date_flag(0);
}

void Palmus::on_pushButton_2_clicked()
{
    listdate.clear();
    update();
    emit get_date_flag(1);
}

void Palmus::on_pushButton_3_clicked()
{
    listdate.clear();
    update();
    emit get_date_flag(2);
}

